<template>
	<m-cpage :list="formList" @extendFunc="extendFunc" @submit="submit"></m-cpage>
</template>

<script>
	export default {
		data() {
			let _this = this;
			return {
				options:{},
				formList:[
					
					
				],
				formList1:[
					{
						label:'请输入常用邮箱',
						placeholder:'请输入',
						tag:'email',
						type:'email',
						rules:{
							type: 'string',
							required: true,
							message: '请输入正确的邮箱',
							trigger: ['blur', 'change'],
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.email(value) 
							},
						},
					},
					{
						label:'密码',
						placeholder:'6-10位，由数字+字母组成',
						tag:'password',
						type:'password',
						icon:'eyes',
						rules:{
							type: 'string',
							required: true,
							message: '6-10位，由数字+字母组成',
							trigger: ['blur', 'change'],
							validator:(rule,value,callback) => {
								return _this.utils.CheckPassWord(value)
							}
						},
					},
					{
						label:'确认密码',
						placeholder:'6-10位，由数字+字母组成',
						tag:'password2',
						type:'password',
						icon:'eyes',
						rules:{
							type: 'string',
							required: true,
							message: '6-10位，由数字+字母组成',
							trigger: ['blur', 'change'],
							validator:(rule,value,callback) => {
								return _this.utils.CheckPassWord(value)
							}
						},
					},
					{
						label:'填写邮箱验证码',
						placeholder:'输入验证码',
						tag:'code',
						type:'email',
						ptag:'email',
						icon:'send',
						isMobile:true,
						text:'发送验证码',
						request_type:"1",
						rules:{
							type: 'string',
							message:'填写正确的邮箱验证码',
							required: true,
							trigger: ['blur', 'change']
						},
					},
					{
						agree:true,
						tag:'aggree',
						classes:'noBorder ptb20'
					},{
						button:true,
						disabled:true,
						submit:true,
						loading:false,
						func:'submit',
						type:'',
						btn:'注册',
						btnStyle:{
							color:'#fff',fontSize:'28rpx',
							background:'#16b7ff',borderRadius:'5rpx',
						},
						classes:'noBorder'
					},
				],
				formList2:[
					{
						label:'请输入常用手机号',
						placeholder:'请输入',
						tag:'mobile',
						type:'mobile',
						rules:{
							type: 'string',
							required: true,
							message: '请输入正确的手机号',
							trigger: ['blur', 'change'],
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value) 
							},
						},
					},
					{
						label:'密码',
						placeholder:'6-10位，由数字+字母组成',
						tag:'password',
						type:'password',
						icon:'eyes',
						rules:{
							type: 'string',
							required: true,
							message: '6-10位，由数字+字母组成',
							trigger: ['blur', 'change'],
							validator:(rule,value,callback) => {
								return _this.utils.CheckPassWord(value)
							}
						},
					},
					{
						label:'确认密码',
						placeholder:'6-10位，由数字+字母组成',
						tag:'password2',
						type:'password',
						icon:'eyes',
						rules:{
							type: 'string',
							required: true,
							message: '6-10位，由数字+字母组成',
							trigger: ['blur', 'change'],
							validator:(rule,value,callback) => {
								return _this.utils.CheckPassWord(value)
							}
						},
					},
					{
						label:'填写手机验证码',
						placeholder:'输入验证码',
						tag:'code',
						type:'mobile',
						ptag:'mobile',
						icon:'send',
						isMobile:true,
						text:'发送验证码',
						cmd:'send_phone_code',
						request_type:"4",
						rules:{
							type: 'string',
							message:'填写正确的手机验证码',
							required: true,
							trigger: ['blur', 'change']
						},
					},
					{
						agree:true,
						tag:'aggree',
						classes:'noBorder ptb20'
					},{
						button:true,
						disabled:true,
						submit:true,
						loading:false,
						func:'submit',
						type:'',
						btn:'注册',
						btnStyle:{
							color:'#fff',fontSize:'28rpx',
							background:'#16b7ff',borderRadius:'5rpx',
						},
						classes:'noBorder'
					},
				],
				obj1:[{
					buttonTwo:true,
					disabled:false,
					func:'goBack',
					login:true,
					classes:'noBorder',
					btnList:[
						{
							btn:'切换手机号注册',
							type:'text',
							func:'extendFunc',
							btnStyle:{
								color:'#26bbff',fontSize:'28rpx'
							},
						},{
							btn:'返回登录',
							type:'text',
							func:'goBack',
							btnStyle:{
								color:'#26bbff',fontSize:'28rpx'
							},
						},
						
					],
				}],
				obj2:[
					{
						button:true,
						disabled:false,
						func:'goBack',
						login:true,
						type:'text',
						btn:'返回登录',
						classes:'noBorder',
						btnStyle:{
							color:'#26bbff',fontSize:'28rpx'
						},
					}
				],
				isPhoneOpen:false,//是否开启手机号注册
				isEmail:true,//模式邮箱模式
			};
		},
		onLoad(options = {}) {
			this.options = options 
			this.getPhoneIsOpen()
			// let rinfo = {
			// 	checkedVal: ['1'],
			// 	user_name: 123,
			// 	pass_word: '456qwe',
			// }
			// let base64Info = Base64.encode(JSON.stringify(rinfo))
			// uni.setStorageSync('rinfo', base64Info)
			// setTimeout(()=> {
			// 	uni.redirectTo({
			// 		url: '/pages/login/login'
			// 	});
			// },1500)
		},
		methods:{
			getPhoneIsOpen(){
				debugger
				try{
					this.$api.phoneIsOpen().then(res => {
						this.isPhoneOpen = res.data
						this.setEmailList()
					})
				}catch(e){
					this.setEmailList()
					//TODO handle the exception
				}
				
			},
			setMobileList(){
				this.obj1[0].btnList[0].btn = '切换邮箱注册'
				this.formList = [...this.formList2,...this.obj1]
				console.log(this.formList)
			},
			setEmailList(){
				this.obj1[0].btnList[0].btn = '切换手机号注册'
				let obj = this.isPhoneOpen?this.obj1:this.obj2
				this.formList = [...this.formList1,...obj]
				// this.formList = this.formList1.push(this.isPhoneOpen?this.obj1:this.obj2)
			},
			extendFunc(){
				debugger
				if(this.isEmail){
					this.setMobileList()
				}else{
					this.setEmailList()
				}
				this.isEmail = !this.isEmail
			},
			submit(val){
				
				let par = {
					...val,
					"pass_word": val.password,
					team:this.options.team || uni.getStorageSync('team')

				}
				let cmd = 'register'
				if(!this.isEmail){
					cmd = 'registerPhone'
					par.phone = par.mobile
				}
				this.$api[cmd](par).then(res => {
					if(res.code == 200){
						uni.showToast({
							title:'注册成功!',
							icon:'none',
						})
						let rinfo = {
							checkedVal: ['1'],
							user_name: par.email || par.phone,
							pass_word: par.pass_word,
						}
						let base64Info = Base64.encode(JSON.stringify(rinfo))
						uni.removeStorageSync('team')
						uni.setStorageSync('rinfo', base64Info)
						setTimeout(()=> {
							uni.redirectTo({
								url: '/pages/login/login'
							});
						},1500)
					}else{
						uni.showToast({
							title:res.message,
							icon:'none',
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
